/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package quanlykho;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author ADMIN
 */
public class ThongKeHangHoa extends javax.swing.JFrame {

    /**
     * Creates new form test
     */
    DefaultTableModel model;
    DefaultTableModel model2;

    public ThongKeHangHoa() {
        initComponents();
        Ketnoi a = new Ketnoi();
        Connection con;
        con = a.connect();
        try {
            model = (DefaultTableModel) jt1.getModel();
            PreparedStatement pst1 = con.prepareCall("Select * from View_PhieuNhap");
            ResultSet rs1 = pst1.executeQuery();
            while (rs1.next()) {
                Vector b = new Vector();
                b.add(rs1.getString(1));
                b.add(rs1.getString(2));

                b.add(rs1.getString(3));
                b.add(rs1.getString(4));
                b.add(rs1.getInt(5));
                b.add(rs1.getInt(6));
                model.addRow(b);
            }
              int num = jt1.getRowCount();
            int s1 = 0;
            int s2 = 0;
            int s3 = 0;
            for (int j = num - 1; j >= 0; j--) {
                s1+=Integer.parseInt(jt1.getValueAt(j, 5).toString());    
            }
            nhap.setText(String.valueOf(s1));
            
            model = (DefaultTableModel) jt2.getModel();
            PreparedStatement pst2 = con.prepareCall("Select * from View_PhieuXuat");
            ResultSet rs2 = pst2.executeQuery();
            while (rs2.next()) {
                Vector b = new Vector();
                b.add(rs2.getString(1));
                b.add(rs2.getString(2));
                b.add(rs2.getString(3));
                b.add(rs2.getString(4));
                b.add(rs2.getString(5));
                b.add(rs2.getString(6));
                b.add(rs2.getString(7));
                b.add(rs2.getInt(8));
                b.add(rs2.getInt(9));
                model.addRow(b);

            }
             num = jt2.getRowCount();
             for (int j = num - 1; j >= 0; j--) {
                s2+=Integer.parseInt(jt2.getValueAt(j, 8).toString());    
            }
           xuat.setText(String.valueOf(s2));
           s3=s2-s1;
           thu.setText(String.valueOf(s3));
             
        } catch (SQLException ex) {
            Logger.getLogger(ThongKeHangHoa.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel7 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        ngay = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        thang = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        nam = new javax.swing.JTextField();
        xem1 = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        mahh = new javax.swing.JTextField();
        xem2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jt1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jt2 = new javax.swing.JTable();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        labnhap3 = new javax.swing.JLabel();
        lablenhap = new javax.swing.JLabel();
        nhap2 = new javax.swing.JLabel();
        nhap = new javax.swing.JTextField();
        xuat = new javax.swing.JTextField();
        thu = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel2.setText("Ngày:");

        jLabel3.setText("Tháng:");

        thang.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                thangActionPerformed(evt);
            }
        });

        jLabel4.setText("Năm:");

        xem1.setText("Xem");
        xem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                xem1ActionPerformed(evt);
            }
        });

        jLabel5.setText("Mã Hàng Hóa:");

        xem2.setText("Xem");
        xem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                xem2ActionPerformed(evt);
            }
        });

        jt1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mã Hàng Hóa", "Mã Phiếu Nhập", "Mã Nhân Viên", "Ngày Nhập", "Số Mặt Hàng", "Tổng Tiền"
            }
        ));
        jScrollPane1.setViewportView(jt1);

        jt2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mã Hàng Hóa", "Mã Phiếu Xuất", "Mã Nhân Viên", "Tên KH", "Địa Chỉ KH", "Số ĐT KH", "Ngày Xuất", "Số Mặt Hàng", "Tổng Tiền"
            }
        ));
        jScrollPane2.setViewportView(jt2);

        jLabel6.setText("Nhập kho:");

        jLabel7.setText("Xuất kho:");

        labnhap3.setText("Tổng tiền Xuất:");

        lablenhap.setText("Tổng tiền Nhập:");

        nhap2.setText("Doanh Thu:");

        javax.swing.GroupLayout jPanel7Layout = new javax.swing.GroupLayout(jPanel7);
        jPanel7.setLayout(jPanel7Layout);
        jPanel7Layout.setHorizontalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel7Layout.createSequentialGroup()
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(ngay, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(thang, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel7Layout.createSequentialGroup()
                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(mahh, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel7Layout.createSequentialGroup()
                                .addComponent(xem2, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(labnhap3, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(nhap2, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel7Layout.createSequentialGroup()
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(nam, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(28, 28, 28)
                                .addComponent(xem1, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lablenhap, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(nhap, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(thu, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(xuat, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 705, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 557, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(51, Short.MAX_VALUE))
        );
        jPanel7Layout.setVerticalGroup(
            jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel7Layout.createSequentialGroup()
                .addGap(51, 51, 51)
                .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel7Layout.createSequentialGroup()
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(ngay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(nam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(xem1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(thang))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(mahh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(xem2)
                            .addComponent(labnhap3, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(xuat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(nhap2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(thu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(1, 1, 1)
                        .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel7Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(nhap, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(lablenhap, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(132, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(24, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void thangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_thangActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_thangActionPerformed

    private void xem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_xem2ActionPerformed
        // TODO add your handling code here:
        Ketnoi a = new Ketnoi();
        Connection con;
        con = a.connect();
        try {
            model = (DefaultTableModel) jt1.getModel();
            int num = jt1.getRowCount();
            for (int j = num - 1; j >= 0; j--) {
                model.removeRow(j);
            }
            
            
            PreparedStatement pst1 = con.prepareCall("Select * from View_PhieuNhap where MaHH =?");
            pst1.setString(1, mahh.getText());
    
            ResultSet rs1 = pst1.executeQuery();
            while (rs1.next()) {
                Vector b = new Vector();
                b.add(rs1.getString(1));
                b.add(rs1.getString(2));

                b.add(rs1.getString(3));
                b.add(rs1.getString(4));
                b.add(rs1.getInt(5));
                b.add(rs1.getInt(6));
                model.addRow(b);
            }
              num = jt1.getRowCount();
            int s1 = 0;
            int s2 = 0;
            int s3 = 0;
            for (int j = num - 1; j >= 0; j--) {
                s1+=Integer.parseInt(jt1.getValueAt(j, 5).toString());    
            }
            nhap.setText(String.valueOf(s1));
            
            model = (DefaultTableModel) jt2.getModel();
            num = jt2.getRowCount();
            for (int j = num - 1; j >= 0; j--) {
                model.removeRow(j);
            }
            PreparedStatement pst2 = con.prepareCall("Select * from View_PhieuXuat where MaHH =?");
            pst2.setString(1, mahh.getText());
            ResultSet rs2 = pst2.executeQuery();
            while (rs2.next()) {
                Vector b = new Vector();
                b.add(rs2.getString(1));
                b.add(rs2.getString(2));
                b.add(rs2.getString(3));
                b.add(rs2.getString(4));
                b.add(rs2.getString(5));
                b.add(rs2.getString(6));
                b.add(rs2.getString(7));
                b.add(rs2.getInt(8));
                b.add(rs2.getInt(9));
                model.addRow(b);

            }
             num = jt2.getRowCount();
             for (int j = num - 1; j >= 0; j--) {
                s2+=Integer.parseInt(jt2.getValueAt(j, 8).toString());    
            }
           xuat.setText(String.valueOf(s2));
           s3=s2-s1;
           thu.setText(String.valueOf(s3));
             
        } catch (SQLException ex) {
            Logger.getLogger(ThongKeHangHoa.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_xem2ActionPerformed

    private void xem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_xem1ActionPerformed
        // TODO add your handling code here:
        
    }//GEN-LAST:event_xem1ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ThongKeHangHoa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ThongKeHangHoa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ThongKeHangHoa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ThongKeHangHoa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ThongKeHangHoa().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jt1;
    private javax.swing.JTable jt2;
    private javax.swing.JLabel lablenhap;
    private javax.swing.JLabel labnhap3;
    private javax.swing.JTextField mahh;
    private javax.swing.JTextField nam;
    private javax.swing.JTextField ngay;
    private javax.swing.JTextField nhap;
    private javax.swing.JLabel nhap2;
    private javax.swing.JTextField thang;
    private javax.swing.JTextField thu;
    private javax.swing.JButton xem1;
    private javax.swing.JButton xem2;
    private javax.swing.JTextField xuat;
    // End of variables declaration//GEN-END:variables
}
